﻿using AppSocketModel;
using AppSocketModel.Consume;
using AppSocketService.Account;
using BCGPSCommon;
using BCGPSMongoData;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;

namespace AppSocketService.Consume
{
    /// <summary>
    /// 设备使用费套餐
    /// </summary>
    public class DeviceUsePayTypeSelectService : ServiceBase, IDeviceUsePayTypeSelectService
    {

        private MongoDBCommand<DeviceUsePayTypeSelectDto> mongoDBCommand = new MongoDBCommand<DeviceUsePayTypeSelectDto>("DeviceUsePayTypeSelect");
        private readonly AccountDto CurrentAccount;

        public DeviceUsePayTypeSelectService(IClaimsAccessor claimsAccesso, ISystemAccountService systemAccountService) : base(claimsAccesso)
        {
            if (claimsAccesso.AccountRole == AccountRole.SYSTEM_PAY)
                CurrentAccount = systemAccountService.GetCurrentAccount().AsAccountDto();
            else
                throw new Exception("登陆账户没有权限!");
            if (CurrentAccount == null)
                throw new Exception("登陆账户异常,请重新登陆!");
        }

        #region 添加
        public DeviceUsePayTypeSelectDto Add(DeviceUsePayTypeSelectRequestDto requestDto)
        {
            var dtoNew = requestDto.As<DeviceUsePayTypeSelectDto>();

            dtoNew.CreateSystemAccountCode = CurrentAccount.AccountName;
            dtoNew.CreateDateTime = DateTime.Now;
            dtoNew.CreateSystemAccountCode = CurrentAccount.AccountCode;
            dtoNew.ModifyDateTime = BCGPSCommon.MongoDBDateTime.GetNowUtc();

            dtoNew.PayAmountActual = Convert.ToDecimal((float)dtoNew.PayAmount * dtoNew.Discount);

            mongoDBCommand.InsertDocumentAsync(dtoNew).GetAwaiter().GetResult();

            return dtoNew;
        }

        #endregion

        #region 修改

        public DeviceUsePayTypeSelectDto Modify(DeviceUsePayTypeSelectPutDto putDto)
        {
            var dto = mongoDBCommand.SelectDocumentAsync(s => s.Id == putDto.Id).GetAwaiter().GetResult();

            if (dto.IsStartUse || dto.IsDiscard || dto.IsOverTime)
                throw new ArgumentException("不能修改已经 [ 启用 | 废弃 |  超时 ] 的数据项!");

            dto.AccountRole = putDto.AccountRole;
            dto.UseMonths = putDto.UseMonths;
            dto.PayAmount = putDto.PayAmount;
            dto.Discount = putDto.Discount;
            dto.PayAmountActual = Convert.ToDecimal((float)putDto.PayAmount * putDto.Discount);
            dto.ModifySystemAccountCode = CurrentAccount.AccountCode;
            dto.ModifyDateTime = DateTime.Now;


            var update = Builders<DeviceUsePayTypeSelectDto>.Update
                    .Set(s => s.AccountRole, dto.AccountRole)
                    .Set(s => s.UseMonths, dto.UseMonths)
                    .Set(s => s.PayAmount, dto.PayAmount)
                    .Set(s => s.Discount, dto.Discount)
                    .Set(s => s.PayAmountActual, dto.PayAmountActual)
                    .Set(s => s.ModifySystemAccountCode, dto.ModifySystemAccountCode)
                    .Set(s => s.ModifyDateTime, dto.ModifyDateTime);
            var ret = mongoDBCommand.UpdateDocumentAsync(s => s.Id == dto.Id, update).GetAwaiter().GetResult();
            if (ret == null)
                throw new ArgumentException("修改失败,请重试!");
            return ret;
        }
        #endregion

        #region 废弃
        public bool SetDiscard(string id)
        {

            var dto = mongoDBCommand.SelectDocumentAsync(s => s.Id == id).GetAwaiter().GetResult();

            if (dto.IsDiscard)
                throw new ArgumentException("不能修改已经 [  废弃  ] 的数据项!");

            var update = Builders<DeviceUsePayTypeSelectDto>.Update
                    .Set(s => s.IsDiscard, true)
                    .Set(s => s.DiscardSystemAccountCode, CurrentAccount.AccountCode)
                    .Set(s => s.DiscardDateTime, BCGPSCommon.MongoDBDateTime.GetNowUtc());
            var ret = mongoDBCommand.UpdateDocumentAsync(s => s.Id == id, update).GetAwaiter().GetResult();
            if (ret == null)
                throw new ArgumentException("修改失败,请重试!");
            return true;
        }
        #endregion

        #region 启用
        public bool SetStartUse(string id)
        {
            var dto = mongoDBCommand.SelectDocumentAsync(s => s.Id == id).GetAwaiter().GetResult();

            if (dto.IsStartUse || dto.IsDiscard || dto.IsOverTime)
                throw new ArgumentException("不能修改已经 [ 启用 | 废弃 |  超时 ] 的数据项!");

            var update = Builders<DeviceUsePayTypeSelectDto>.Update
                    .Set(s => s.IsStartUse, true)
                    .Set(s => s.StartUseSystemAccountCode, CurrentAccount.AccountCode)
                    .Set(s => s.StartUseDateTime, BCGPSCommon.MongoDBDateTime.GetNowUtc());
            var ret = mongoDBCommand.UpdateDocumentAsync(s => s.Id == id, update).GetAwaiter().GetResult();
            if (ret == null)
                throw new ArgumentException("修改失败,请重试!");
            return true;
        }
        #endregion

        #region GetById
        public DeviceUsePayTypeSelectDto GetById(string id)
        {
            return mongoDBCommand.SelectDocumentAsync(s => s.Id == id).GetAwaiter().GetResult();
        }
        #endregion

        #region GetList
        public PageableList<DeviceUsePayTypeSelectDto> GetList(bool? isStartUse, bool? isDiscard, bool? isTimeOver, bool desc, int index, int size)
        {
            index = index > 0 ? index - 1 : 0;

            List<string> wheres = new List<string>();

            if (isStartUse.HasValue)
                wheres.Add($"IsStartUse:{isStartUse.Value.ToString().ToLower()}");
            if (isDiscard.HasValue)
                wheres.Add($"isDiscard:{isDiscard.Value.ToString().ToLower()}");
            if (isTimeOver.HasValue)
                wheres.Add($"isTimeOver:{isTimeOver.Value.ToString().ToLower()}");

            string str = "{" + string.Join(" , ", wheres) + "}";

            return mongoDBCommand.SelectDocumentsAsync(str, s => s.CreateDateTime, false, index * size, size).GetAwaiter().GetResult();
        }
        #endregion
    }
}
